package com.ysz.controller;

import com.ysz.globalexception.components.BaseException;
import com.ysz.globalexception.exums.ExceptionEnum;
import com.ysz.globalexception.retresult.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @ClassName TestController
 * @Description TODO
 * @Data 17:45
 * @Version 1.0
 * @Author ysz
 */
@RestController
public class TestController {

    @Autowired
    private S1Mapper s1Mapper;


    @GetMapping("/test1")
    public ReturnResult test1() {
        //测试正常return
        return new ReturnResult(3024, "error", "data");
    }

    @GetMapping("/test2")
    public ReturnResult test2() throws Throwable {
        //测试断言工具类
        Object obj = null;
        ExceptionEnum.ERROR_3.notNull(obj);
        return new ReturnResult(200, "ok", "data");
    }


    @GetMapping("/test3")
    public ReturnResult test3() throws Throwable {
        //测试断言工具类
        Object obj = null;
        ExceptionEnum.ERROR_3.notNull(new TestException(1100, "指定异常====》TestException异常测试"), obj);
        return new ReturnResult(200, "ok", "data");
    }


    @GetMapping("/test4")
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult test4() throws Throwable {
        //测试回滚
        int insert = s1Mapper.insert(S1.builder()
                .expireDuration(1L)
                .expireTime(new Date()).insertTime(new Date())
                .orderNo("1")
                .orderNote("2")
                .orderStatus(1).build());
        if (insert == 1) {
            System.out.println("新增成功");
        }
        Object obj = null;
        ExceptionEnum.ERROR_3.notNull(obj);
        return new ReturnResult(200, "ok", "data");
    }


    @GetMapping("/test5")
    public ReturnResult test5() throws Throwable {
        //测试断言工具类
        Object obj = null;
        ExceptionEnum.ERROR_3.notNull(new NullPointerException("指定异常====》空指针异常测试"), obj);
        return new ReturnResult(200, "ok", "data");
    }


    @GetMapping("/test6")
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult test6() throws Throwable {
        //测试正常插入
        int insert = s1Mapper.insert(S1.builder()
                .expireDuration(1L)
                .expireTime(new Date()).insertTime(new Date())
                .orderNo("1")
                .orderNote("2")
                .orderStatus(1).build());
        if (insert == 1) {
            System.out.println("新增成功");
        }
        return new ReturnResult(200, "ok", "data");
    }
}
